package com.qqtech.qquan.homemaking.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.controller.BaseControllerImpl;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.homemaking.enums.HomemakingBaseConst;
import com.qqtech.qquan.homemaking.enums.HomemakingTimeTypeDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingBaseVo;
import com.qqtech.qquan.homemaking.model.HomemakingTime;
import com.qqtech.qquan.homemaking.model.HomemakingTimeQuery;
import com.qqtech.qquan.homemaking.model.HomemakingTimeVo;
import com.qqtech.qquan.homemaking.service.HomemakingBaseService;
import com.qqtech.qquan.homemaking.service.HomemakingTimeService;

@Controller
@RequestMapping(value = "/homemaking/time")
public class HomemakingTimeController extends
		BaseControllerImpl<HomemakingTime, HomemakingTimeQuery> {
	@Resource
	private HomemakingTimeService homemakingTimeService;
	
	@Resource
	private HomemakingBaseService homemakingBaseService;

	@Override
	protected BaseService<HomemakingTime> getBaseService() {
		return homemakingTimeService;
	}

	@RequestMapping(value = "/findByDate")
	@ResponseBody
	public Result findByDate(@RequestBody HomemakingTimeQuery q) {
		Result result = new Result();
		// 日期
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		List<Map<String, String>> dateList = new ArrayList<Map<String, String>>();
		Calendar calendar1 = Calendar.getInstance();
		result.put("nowHour", calendar1.get(Calendar.HOUR_OF_DAY)); // 当前时间的小时
		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, 7);
		int index = 0;
		while (calendar1.before(calendar2)) {
			Map<String, String> map = new HashMap<String, String>();
			int w = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
	        if (w < 0) {
	        	w = 0;
	        }
	        String d = weekDays[w];
	        if (index == 0) {
	        	d = "今天";
	        } else if (index == 1) {
	        	d = "明天";
	        }
			map.put("date", TimeUtil.getStringDateByFormat(calendar1.getTime(),
					"yyyy-MM-dd"));
			map.put("dateStr", TimeUtil.getStringDateByFormat(calendar1.getTime(),
					"MM-dd")+"\n"+d);
			map.put("border", "f2f3f6FF,1,4");
			map.put("fontColor", "949eaaFF");
			dateList.add(map);
			calendar1.add(Calendar.DAY_OF_YEAR, 1);
			index++;
		}
		result.put("dateList", dateList);
		String serviceDateStr = null;
		if (q.getServiceDateStr() == null) {
			serviceDateStr = dateList.get(0).get("date");
		} else {
			serviceDateStr = q.getServiceDateStr();
		}
		List<HomemakingTimeVo> selectHourList = homemakingTimeService
				.findByDate(q.getBaseId(), serviceDateStr, null);
		result.put("selectHourList", selectHourList);
		result.put("serviceDateStr", serviceDateStr);
		return result;
	}

	@RequestMapping(value = "/findByDateAndType")
	@ResponseBody
	public Result findByDateAndType(@RequestBody HomemakingTimeQuery q) {
		Result result = new Result();
		HomemakingBaseVo homemakingBaseVo = homemakingBaseService
				.getById(q.getBaseId());
		// 日期
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		List<Map<String, String>> dateList = new ArrayList<Map<String, String>>();
		Calendar calendar1 = Calendar.getInstance();
		result.put("nowHour", calendar1.get(Calendar.HOUR_OF_DAY)); // 当前时间的小时
		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, 7);
		int index = 0;
		while (calendar1.before(calendar2)) {
			Map<String, String> map = new HashMap<String, String>();
			int w = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
	        if (w < 0) {
	        	w = 0;
	        }
	        String d = weekDays[w];
	        if (index == 0) {
	        	d = "今天";
	        } else if (index == 1) {
	        	d = "明天";
	        }
			map.put("date", TimeUtil.getStringDateByFormat(calendar1.getTime(),
					"yyyy-MM-dd"));
			map.put("dateStr", TimeUtil.getStringDateByFormat(calendar1.getTime(),
					"MM-dd")+"\n"+d);
			dateList.add(map);
			calendar1.add(Calendar.DAY_OF_YEAR, 1);
			index++;
		}
		result.put("dateList", dateList);
		String serviceDateStr = null;
		if (q.getServiceDateStr() == null) {
			serviceDateStr = dateList.get(0).get("date");
		} else {
			serviceDateStr = q.getServiceDateStr();
		}
		// 已预约时间
		List<HomemakingTimeVo> orderHourList = homemakingTimeService
				.findByDate(q.getBaseId(), serviceDateStr, HomemakingTimeTypeDictEnum.ORDERED.getCode());
		List<HomemakingTimeVo> restHourList = null;
		if (homemakingBaseVo.getTypeId().intValue() == HomemakingBaseConst.RI_CHANG_BAO_JIE_ID) {//日常保洁是上班时间
			restHourList = homemakingTimeService.findByDate(q.getBaseId(), serviceDateStr, HomemakingTimeTypeDictEnum.WORK.getCode());
		} else {
			// 休息时间
			restHourList = homemakingTimeService.findByDate(q.getBaseId(), serviceDateStr, HomemakingTimeTypeDictEnum.REST.getCode());
		}
		result.put("orderHourList", orderHourList);
		result.put("restHourList", restHourList);
		result.put("serviceDateStr", serviceDateStr);
		return result;
	}

	/**
	 * 保存休息时间
	 * 
	 * @param v
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/addRestTime")
	@ResponseBody
	public Result addRestTime(@RequestBody HomemakingTimeVo v) {
		Result result = new Result();
		if (v == null || v.getBaseId() == null
				|| StringUtil.isBlank(v.getServiceDateStr())) {
			result.addErr("参数不能为空");
		}
		if (result.checkForm()) {
			HomemakingBaseVo homemakingBaseVo = homemakingBaseService
					.getById(v.getBaseId());
			List<HomemakingTime> list = new ArrayList<HomemakingTime>();
			String[] hours = v.getSeviceHourStrs().split(";");
			for (String h : hours) {
				if (!StringUtil.isBlank(h)) {
					HomemakingTime inser = new HomemakingTime();
					inser.setBaseId(v.getBaseId());
					inser.setServiceDateStr(v.getServiceDateStr());
					if (homemakingBaseVo.getTypeId().intValue() == HomemakingBaseConst.RI_CHANG_BAO_JIE_ID) {//日常保洁是上班时间
						inser.setType(HomemakingTimeTypeDictEnum.WORK.getCode());
					} else {
						inser.setType(HomemakingTimeTypeDictEnum.REST.getCode());
					}
					inser.setServiceHourStr(h);
					inser.setOrderNo("");
					list.add(inser);
				}
			}
			result = homemakingTimeService.addRestTime(v.getBaseId(),v.getServiceDateStr(),list);
		}
		return result;
	}
}
